package com.java.shop.product.controller;

import com.java.common.utils.R;
import com.java.shop.product.entity.CategoryEntity;
import com.java.shop.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;



/**
 * 
 *
 * @author cxq
 * @email 137512476@qq.com
 * @date 2021-03-26 11:19:52
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
//    @RequestMapping("/list")
//    //@RequiresPermissions("product:category:list")
//    public R list(@RequestParam Map<String, Object> params){
//        PageUtils page = categoryService.queryPage(params);
//        return R.ok().put("page", page);
//    }

    @GetMapping("/tree/list")
    public R list(){
        List<CategoryEntity> list = categoryService.listWithTree();
        //TODO 测试数据太多，测试先截取
        list = list.subList(0,3);
        return R.ok().put("data",list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);
        return R.ok().put("id",category.getCatId().toString());
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateDetail(category);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update/sort")
    //@RequiresPermissions("product:category:update")
    public R updateSort(@RequestBody List<CategoryEntity> categorylist){
        boolean b = categoryService.updateBatchById(categorylist);
        return b ? R.ok() : R.error();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){
        //TODO 删除前判断其他地方是否有被引用
		categoryService.removeByIds(Arrays.asList(catIds));
        return R.ok().put("catids",catIds);
    }

}
